# coding=utf-8

from flask import request, jsonify
from application import app, redis
from app.interceptors.Auth.Utils import CompileIgnore
from app.model.redis.user import User as UserRedis


def execute(path):
    ignore_urls = app.config['API_IGNORE_URLS']
    if CompileIgnore(path, ignore_urls):
        return
    token = request.headers.get("token")
    if token is None:
        resp = {'code': -1, 'msg': 'Token失踪~', 'data': {}}
        return jsonify(resp)

    auth_info = token.split("#")
    if len(auth_info) != 2:
        resp = {'code': -2, 'msg': 'Token异常~', 'data': {}}
        return jsonify(resp)

    check_result = check_online(token)
    if not check_result:
        resp = {'code': -3, 'msg': '接口被限制~', 'data': {}}
        return jsonify(resp)
    return


def check_online(token):
    result = redis.sismember(UserRedis.single_login_set.key, token)
    return result
